package com.example.graduationdesign.controller;

import com.example.graduationdesign.mapper.CommonMapper;
import com.example.graduationdesign.pojo.dao.ArticleDao;
import com.example.graduationdesign.pojo.dao.UserDao;
import com.example.graduationdesign.pojo.dao.WordDao;
import com.example.graduationdesign.pojo.dto.*;
import com.example.graduationdesign.pojo.vo.PageInfo;
import com.example.graduationdesign.pojo.vo.Result;
import com.example.graduationdesign.pojo.vo.ResultCode;
import com.example.graduationdesign.pojo.vo.UserVo;
import com.example.graduationdesign.service.AdminService;
import com.example.graduationdesign.service.ConsumerService;
import com.example.graduationdesign.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@Slf4j
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private CommonMapper commonMapper;
    @Autowired
    private ConsumerService consumerService;

    /**
     * 对该系统用户进行管理
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/consumer/page")
    Result<PageInfo<UserDao>> SelectUser(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize){
        System.out.println(pageNo);
        System.out.println("-------------------------------------------");
        System.out.println(adminService.SelectUser(pageNo, pageSize));
        System.out.println("------------------------------------");
        return Result.success(adminService.SelectUser(pageNo, pageSize));
    }


    /**
     * 对文章内容进行管理
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/article/page")
    Result<PageInfo<ArticleDao>> SelectArticle(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize){

        return Result.success(adminService.SelectArticle(pageNo, pageSize));
    }


    /**
     * 对该系统的单词进行管理
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/word/page")
    Result<PageInfo<WordDao>> SelectWord(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize){
        System.out.println(pageNo);
        System.out.println("-------------------------------------------");
        System.out.println(adminService.SelectWord(pageNo, pageSize));
        System.out.println("------------------------------------");
        return Result.success(adminService.SelectWord(pageNo, pageSize));
    }


    /**
     * 用于获取活跃用户人数
     * @return
     */
    @GetMapping("/liveStudent")
    Result<Integer> GetLiveStudent(){

        return Result.success(adminService.GetLiveStudent());
    }


    /**
     * 用于获取今日打卡人数
     * @return
     */
    @GetMapping("/check")
    Result<Integer> GetCheckStudent(){

        return Result.success(adminService.GetCheckCount());
    }


    /**
     * 用于实现管理员对文章信息进行查询
     * @param articleId
     * @return
     */
    @PostMapping("/article/search")
    Result<List<ArticleDao>> SearchArticle(@RequestParam("articleId") Long articleId){
        System.out.println(articleId+"12333333333");
        List<ArticleDao> articleDao = adminService.SearchArticle(articleId);
        return Result.success(articleDao);
    }


    /**
     * 用于实现增加文章的功能
     * @param image
     * @param title
     * @param content
     * @param author
     * @return
     */
    @PostMapping("/article/add")
    Result<Void> AddArticle(@RequestParam("image") MultipartFile image,@RequestParam("title") String title,@RequestParam("content") String content,@RequestParam("author") String author){
        System.out.println(image+"12333333333");

        String picturePath = CommonUtil.uploadFile(image);

        adminService.InsertArticle(author,content,picturePath,title);
        return Result.success();
    }


    /**
     * 用于实现有图像修改的文章内容的修改
     * @param image
     * @param title
     * @param content
     * @param author
     * @param code
     * @return
     */
    @PostMapping("/article/edit1")
    Result<Void> EditArticle1(@RequestParam("image") MultipartFile image,@RequestParam("title") String title,@RequestParam("content") String content,@RequestParam("author") String author,@RequestParam("code") Long code){
        System.out.println(image+"12333333333");

        String picturePath = CommonUtil.uploadFile(image);

        adminService.UpdateArticle1(code,author,content,picturePath,title);
        return Result.success();
    }

    /**
     * 用于实现无图像修改的文章内容的修改
     * @param title
     * @param content
     * @param author
     * @param code
     * @return
     */
    @PostMapping("/article/edit2")
    Result<Void> EditArticle2(@RequestParam("title") String title,@RequestParam("content") String content,@RequestParam("author") String author,@RequestParam("code") Long code){
        adminService.UpdateArticle2(code,author,content,title);
        return Result.success();
    }


    /**
     * 用于删除文章的操作
     * @param code
     * @return
     */
    @PostMapping("/article/delete")
    Result<Void> DeleteArticle(@RequestParam("code") Long code){
        adminService.DeleteArticle(code);
        return Result.success();
    }


    /**
     * 用于获取用户在某阶段的排名情况
     * @param selectedValue
     * @return
     */
    @PostMapping("/student/rank")
    Result<List<RankDto>> GetCheckStudent(@RequestParam("selectedValue") String selectedValue){
        System.out.println(selectedValue);

        List<RankDto> rankDtos = new ArrayList<>();
        if(selectedValue.equals("小学")){
            Integer count =1;
            Integer wordCount = consumerService.GetWordNumber(count);
            rankDtos = adminService.GetStudentRanking(count);
            for(RankDto rankDto:rankDtos){
                rankDto.setCount(wordCount);
            }
            return Result.success(rankDtos);
        } else if (selectedValue.equals("初中")) {
            Integer count = 2;
            Integer wordCount = consumerService.GetWordNumber(count);
            rankDtos = adminService.GetStudentRanking(count);
            for(RankDto rankDto:rankDtos){
                rankDto.setCount(wordCount);
            }
            return Result.success(rankDtos);
        } else if (selectedValue.equals("高中")) {
            Integer count = 3;
            Integer wordCount = consumerService.GetWordNumber(count);
            rankDtos = adminService.GetStudentRanking(count);
            for(RankDto rankDto:rankDtos){
                rankDto.setCount(wordCount);
            }
            return Result.success(rankDtos);
        } else if (selectedValue.equals("四级")) {
            Integer count = 4;
            Integer wordCount = consumerService.GetWordNumber(count);
            rankDtos = adminService.GetStudentRanking(count);
            for(RankDto rankDto:rankDtos){
                rankDto.setCount(wordCount);
            }
            return Result.success(rankDtos);
        } else if (selectedValue.equals("六级")) {
            Integer count = 5;
            Integer wordCount = consumerService.GetWordNumber(count);
            rankDtos = adminService.GetStudentRanking(count);
            for(RankDto rankDto:rankDtos){
                rankDto.setCount(wordCount);
            }
            return Result.success(rankDtos);
        } else if (selectedValue.equals("考研")) {
            Integer count = 6;
            Integer wordCount = consumerService.GetWordNumber(count);
            rankDtos = adminService.GetStudentRanking(count);
            for(RankDto rankDto:rankDtos){
                rankDto.setCount(wordCount);
            }
            return Result.success(rankDtos);
        } else if (selectedValue.equals("雅思")) {
            Integer count = 7;
            Integer wordCount = consumerService.GetWordNumber(count);
            rankDtos = adminService.GetStudentRanking(count);
            for(RankDto rankDto:rankDtos){
                rankDto.setCount(wordCount);
            }
            return Result.success(rankDtos);
        }else {
            Integer count = 8;
            Integer wordCount = consumerService.GetWordNumber(count);
            rankDtos = adminService.GetStudentRanking(count);
            for(RankDto rankDto:rankDtos){
                rankDto.setCount(wordCount);
            }
            return Result.success(rankDtos);
        }
    }


    /**
     * 用于管理员修改用户的基本信息
     * @param userId
     * @param age
     * @param sex
     * @param phone
     * @param userName
     * @param email
     * @return
     */
    @PostMapping("/user/update")
    Result<Void> UpdateUserInfo(@RequestParam("userId") Long userId,@RequestParam("age")Integer age,@RequestParam("sex") Integer sex,@RequestParam("phone") String phone,@RequestParam("userName") String userName,@RequestParam("email") String email){
        adminService.UpdateUserInfo(userId, sex, age, userName, phone, email);
        return Result.success();
    }


    /**
     * 用于管理员逻辑删除用户的基本信息
     * @param userId
     * @param isActive
     * @return
     */
    @PostMapping("/user/delete")
    Result<Void> UpdateUserInfo(@RequestParam("userId") Long userId,@RequestParam("isActive") Integer isActive){
        adminService.DeleteUserInfo(userId, isActive);
        return Result.success();
    }

    /**
     * 用于实现查询用户信息的功能
     * @param userId
     * @param userName
     * @return
     */
    @PostMapping("/user/search")
    Result<List<UserDao>> SearchUserInfo(@RequestParam("userId") Long userId,@RequestParam("userName") String userName){
        List<UserDao> userDaos = adminService.SearchUserInfo(userId, userName);
        return Result.success(userDaos);
    }


    /**
     * 用于实现管理员修改单词信息的功能
     * @param wordDao
     * @return
     */
    @PostMapping("/word/update")
    Result<Void> UpdateWordInfo(@RequestBody WordDao wordDao){
        adminService.UpdateWordInfo(wordDao);
        return Result.success();
    }


    /**
     * 用于管理员添加单词的功能
     * @param wordDao
     * @return
     */
    @PostMapping("/word/add")
    Result<Void> AddWordInfo(@RequestBody WordDao wordDao){
        adminService.AddWordInfo(wordDao);
        return Result.success();
    }


    /**
     * 用于实现管理员查询单词的信息
     * @param selectedOption
     * @param searchInput
     * @return
     */
    @PostMapping("/word/search")
    Result<List<WordDao>> SearchWordInfo(@RequestParam("selectedOption") String selectedOption,@RequestParam("searchInput") String searchInput){
        if(selectedOption.equals("name")){
            List<WordDao> wordDaos = adminService.SearchWordInfoByName(searchInput);
            return Result.success(wordDaos);
        } else if (selectedOption.equals("stage")) {
            List<WordDao> wordDaos = adminService.SearchWordInfoByStage(searchInput);
            return Result.success(wordDaos);
        }else {
            List<WordDao> wordDaos = adminService.SearchWordInfoByDifficulty(searchInput);
            return Result.success(wordDaos);
        }
    }



    @PostMapping("/user/analysis")
    Result<DataAnalysisDto> GetDataAnalysis(@RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate){
        System.out.println(startDate+" "+endDate);
        DataAnalysisDto dataAnalysisDto = adminService.GetDataAnalysis(startDate,endDate);
        return Result.success(dataAnalysisDto);
    }


    /**
     * 用于获取某个地区的用户人数
     * @return
     */
    @GetMapping("/city")
    Result<List<CityDto>> GetCity(){

        List<CityDto>  cityDtos = adminService.GetCity();
        return Result.success(cityDtos);
    }

    /**
     * 用于实现批量向数据库中添加单词数据的接口
     * @param wordDaos
     * @return
     */
    @PostMapping("/batchaddwords")
    Result<Integer> SetData(@RequestBody List<WordDao> wordDaos) {
        Integer count = adminService.SetData(wordDaos);
        return Result.success(count);
    }


    /**
     * 用于实现管理员对验证码的获取操作
     * @param request
     * @return
     */
    @PostMapping("/email/send")
    public Result<String> sendModifyAdminEmail(@RequestBody Map<String,String> request) {
        ModifyAdminEmailDto modifyAdminEmailDto = new ModifyAdminEmailDto();
        modifyAdminEmailDto.setEmail(request.get("email"));
        modifyAdminEmailDto.setNewEmail(request.get("newEmail"));
        System.out.println(modifyAdminEmailDto);
        log.info("接收信息：{}", modifyAdminEmailDto);
        if (!Optional.ofNullable(modifyAdminEmailDto.getEmail()).isPresent()) {
            return Result.failure(ResultCode.PARAM_IS_NULL);

        }
//        System.out.println(adminService.sendModifyAdminEmail(modifyAdminEmailDto.getEmail(),modifyAdminEmailDto.getNewEmail()));
        return Result.success(adminService.sendModifyAdminEmail(modifyAdminEmailDto.getEmail(),modifyAdminEmailDto.getNewEmail()));
    }


    /**
     * 用于修改管理员的邮箱email
     * @param request
     * @return
     */
    @PostMapping("/email/update")
    public Result<Void> updateAdminEmailInformation(@RequestBody Map<String,String> request) {
        ModifyAdminEmailDto modifyAdminEmailDto = new ModifyAdminEmailDto();
        modifyAdminEmailDto.setNewEmail(request.get("newEmail"));
        modifyAdminEmailDto.setUserId(Long.parseLong(request.get("userId")));
        modifyAdminEmailDto.setVerificationCode(request.get("verificationCode"));
        System.out.println(modifyAdminEmailDto);
        if (!Optional.ofNullable(modifyAdminEmailDto).isPresent()) {
            return Result.failure(ResultCode.PARAM_IS_NULL);
        }

        if(commonMapper.GetIsExistEmail(modifyAdminEmailDto.getNewEmail()).size()==0){
            adminService.updateAdminEmailInformation(modifyAdminEmailDto);
            return Result.success();
        }
        return Result.failure(ResultCode.EMAIL_IS_EXIST);
    }
}
